package fun.eliauk.treedemo.utils.encrypt;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import java.util.Base64;

@ConditionalOnProperty(value = "qqzj.encrypt", havingValue = "base64")
@Service
public class Base64EncryptService implements EncryptService {
    @Override
    public String encrypt(String content) {
        try {
            String result = Base64.getEncoder().encodeToString(content.getBytes("UTF-8"));
            return result;
        } catch (Exception e) {
            throw new RuntimeException("encrypt fail!", e);
        }
    }

    @Override
    public String decrypt(String content) {
        try {
            byte[] asBytes = Base64.getDecoder().decode(content);
            String result = new String(asBytes, "UTF-8");
            return result;
        } catch (Exception e) {
            throw new RuntimeException("decrypt fail!", e);
        }
    }
}